package wzh.crm.ui.contact;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import java.sql.SQLException;

import wzh.crm.R;
import wzh.crm.bl.BL;
import wzh.crm.vo.ContactVo;
import wzh.crm.vo.ProductVo;

public class ContactDetailsActivity extends AppCompatActivity {
    ContactVo model;
    boolean editable;

    EditText 联系人;
    EditText 手机号码;
    EditText 固话号码;
    EditText 对应客户;
    EditText 年龄;
    EditText 邮箱;
    EditText 地址;
    EditText 邮编;
    EditText QQ;
    EditText 微信;
    EditText 旺旺;
    EditText 备注;
    Button 保存;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_details);

        loadViews();
        readIntent();
        displayModel();

        if (!editable) {
            disableEdit();
        }
    }

    void loadViews() {
        联系人 = (EditText) findViewById(R.id.联系人);
        手机号码 = (EditText) findViewById(R.id.手机号码);
        固话号码 = (EditText) findViewById(R.id.固话号码);
        对应客户 = (EditText) findViewById(R.id.对应客户);
        年龄 = (EditText) findViewById(R.id.年龄);
        邮箱 = (EditText) findViewById(R.id.邮箱);
        地址 = (EditText) findViewById(R.id.地址);
        邮编 = (EditText) findViewById(R.id.邮编);
        QQ = (EditText) findViewById(R.id.QQ);
        微信 = (EditText) findViewById(R.id.微信);
        旺旺 = (EditText) findViewById(R.id.旺旺);
        备注 = (EditText) findViewById(R.id.备注);
        保存 = (Button) findViewById(R.id.保存);
    }

    void disableEdit() {
        联系人.setFocusable(false);
        手机号码.setFocusable(false);
        固话号码.setFocusable(false);
        对应客户.setFocusable(false);
        年龄.setFocusable(false);
        邮箱.setFocusable(false);
        地址.setFocusable(false);
        邮编.setFocusable(false);
        QQ.setFocusable(false);
        微信.setFocusable(false);
        旺旺.setFocusable(false);
        备注.setFocusable(false);
        保存.setVisibility(View.INVISIBLE);
    }

    void readIntent() {
        editable = getIntent().getBooleanExtra("EDITABLE", true);
        model = (ContactVo) getIntent().getSerializableExtra("MODEL");
        if (model == null) {
            model = new ContactVo();
        }
    }

    void updateModel() {
        model.set姓名(联系人.getText().toString());
        model.set手机(手机号码.getText().toString());
        model.set电话(固话号码.getText().toString());
        model.set所属客户id(Integer.parseInt(对应客户.getText().toString()));
        model.set年龄(Integer.parseInt(年龄.getText().toString()));
        model.set邮箱(邮箱.getText().toString());
        model.set地址(地址.getText().toString());
        model.set邮编(邮编.getText().toString());
        model.setQQ号(QQ.getText().toString());
        model.set微信号(微信.getText().toString());
        model.set旺旺号(旺旺.getText().toString());
        model.set备注(备注.getText().toString());
    }

    void displayModel() {
        this.setTitle(model.get姓名());
        联系人.setText(model.get姓名());
        手机号码.setText(model.get手机());
        固话号码.setText(model.get电话());
        对应客户.setText(""+model.get所属客户id());
        年龄.setText(""+model.get年龄());
        邮箱.setText(model.get邮箱());
        地址.setText(model.get地址());
        邮编.setText(model.get邮编());
        QQ.setText(model.getQQ号());
        微信.setText(model.get微信号());
        旺旺.setText(model.get旺旺号());
        备注.setText(model.get备注());
    }

    public void delete(View view) {
        AsyncTask<Object,Object,Object> asyncTask= new AsyncTask<Object, Object, Object>() {
            @Override
            protected Object doInBackground(Object... params) {
                try {
                    BL.contactBL().delete(model);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                return null;
            }
        };
        asyncTask.execute(new Object());
        this.finish();
    }

    public void save(View view) {
        updateModel();
        AsyncTask<Object,Object,Object> asyncTask= new AsyncTask<Object, Object, Object>() {
            @Override
            protected Object doInBackground(Object... params) {
                try {
                    BL.contactBL().save(model);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                return null;
            }
        };
        asyncTask.execute(new Object());
        this.finish();
    }
}
